package com.fh.fruit.controller.retrospect;


import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.fruit.service.period.PeriodManager;
import com.fh.fruit.util.OSSUtils;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;
import com.fh.util.TwoDimensionCode;

/** 
 * 类名称：ToolController 系统工具
 * 创建人：FH Q313596790
 * 修改时间：2015年11月23日
 * @version
 */
@Controller
@RequestMapping(value="/retrospect")
public class ToolController extends BaseController {
	
	@Resource(name = "periodService")
	private PeriodManager periodService;
	
	@Value("${domain}")
	private String domain;
	
	@Value("${h5_domain}")
	private String h5_domain;
	
	/**二维码页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goTwoDimensionCode")
	public ModelAndView goTwoDimensionCode() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map<String, Object>> baseList = periodService.getBaseList();
		List<Map<String, Object>> goodsList = periodService.getGoodsList();
		mv.setViewName("farm/retrospect/twoDimensionCode");
		mv.addObject("baseList", baseList);
		mv.addObject("goodsList", goodsList);
		mv.addObject("pd", pd);
		return mv;
	}
	
	/**
	 *	生成二维码
	 * @param args
	 * @throws Exception
	 */
	@RequestMapping(value="/createTwoDimensionCode")
	@ResponseBody
	public Object createTwoDimensionCode(){
		Map<String,String> map = new HashMap<String,String>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String errInfo = "success";
		String time = pd.getString("time");	
		String goodsId = pd.getString("goodsId");
		String base_id = pd.getString("base_id");
		String url=null;
		if(null == time){
			errInfo = "error";
		} else if("0".equals(base_id)){
			errInfo = "error";
		} else if("0".equals(goodsId)){
			errInfo = "error";
		} else{
			try {
				int pid=periodService.getPerIodId(pd);
				if(pid==0){
					errInfo = "error";
				}else{
					String content=h5_domain+"/H5MtncRecord?code="+pid;
					InputStream is=TwoDimensionCode.encodeZXcode(content, "png");
					String  fileName="fruit/retrospect/"+base_id+"/"+goodsId+"/"+pid+"/twoCode.png";
					 Map<String, Object> resMap= OSSUtils.pictureUploadI(fileName,is);
					 if(resMap!=null){
						 if(resMap.get("url")!= null){
							 url=(String)resMap.get("url");
						 }else{
							 errInfo = "error";
						 }
					 }
				}//执行生成二维码
			} catch (Exception e) {
				errInfo = "error";
			}
		}
		map.put("result", errInfo);						//返回结果
		map.put("encoderImgId", url);			//二维码图片名
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**农务信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/farmWork")
	public ModelAndView goFarmWork(Page page) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		page.setPd(pd);
		List<Map<String, Object>> baseList = periodService.getBaseList();
		List<Map<String, Object>> farmList = periodService.getFarmWorkList(page);
		mv.setViewName("farm/retrospect/farmWork_list");
		mv.addObject("farmList", farmList);
		mv.addObject("baseList", baseList);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());
		return mv;
	}
	
	/**农务信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		Map<String, Object> farm = periodService.getFarmWork(pd);
		mv.setViewName("farm/retrospect/farmWord_edit");
		mv.addObject("farm", farm);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());
		return mv;
	}
	
	/**
	 * 审核状态
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateSHState")
	public ModelAndView updateSHState(HttpServletRequest requset) throws Exception {
		logBefore(logger, Jurisdiction.getUsername() + "审核状态");
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		pd.put("createId", user.getUSER_ID()); // 创建人
		pd.put("createTime", DateUtil.getTime()); // 创建时间
	  /*  String pic=pd.getString("picture");
		String p[]=pic.split(",");
		String picture="";
		for(int i=0;i<p.length;i++){
			if((i+1)==p.length){
				picture+=p[i]+"||01";
			}else{
				picture+=p[i]+"||01,";
			}
		}
		pd.put("picture", picture);*/
		int num = periodService.updateSHState(pd);
		if(num !=1){
			mv.addObject("msg","修改失败");
		}
		mv.setViewName("save_result");
		return mv;
	}
	
}
